/*
 * @Author: your name
 * @Date: 2021-07-03 15:15:26
 * @LastEditTime: 2021-07-03 17:34:10
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \Skip\assets\script\Hero.ts
 */
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Game from "./Game";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Hero extends cc.Component {


    // LIFE-CYCLE CALLBACKS:

    onLoad() {

    }


    start() {

    }
    onCollisionEnter(other: cc.Collider, self: cc.Collider) {
        let blockJS = other.node.parent.getComponent('block');
        let isCenter = other.tag === 10 && self.tag === 1000;
        let isBlockIn = other.tag === 11 && self.tag === 1000
        // 判断是否已经得分
        if (!blockJS) return;
        if (blockJS.score === 0) {
            if (isCenter) {
                blockJS.score = 2;
                blockJS.isConllision = true;
                Game.inst.updateScore(2)
                cc.log("得2分")
            } else if (isBlockIn) {
                blockJS.score = 1;
                blockJS.isConllision = true;
                Game.inst.updateScore(1)
                cc.log("得1分")
            }
        }
        if (Game.inst.isGetScore === -1) {
            if (isCenter) {
                Game.inst.isGetScore = 2
            } else if (isBlockIn) {
                Game.inst.isGetScore = 1
            } else {
                // 获取 hero 到 块点的向量，判断x的征服来判断 旋转位置
                let pointPosition = blockJS.position;
                let heroPosition = Game.inst.hero.getPosition();
                let positionSub = pointPosition.sub(heroPosition)
                let angle = positionSub.x > 0 ? -30 : 30
                cc.tween(Game.inst.hero)
                    .by(0.3, { angle: -angle })
                    .call(() => {
                        Game.inst.isGetScore = 0
                    })
                    .start()
            }

        }

    }
    // update (dt) {}
}
